package MyString;

import javax.swing.plaf.IconUIResource;

public class Leetcode0791 {
    public static String customSortString(String S, String T) {
        StringBuilder sb = new StringBuilder();
        int[] ch = new int[26];

        for (int ii = 0; ii < T.length(); ii++) {
            ch[T.charAt(ii) - 'a']++;
        }

        for(int ii = 0; ii < 26; ii++){
            System.out.print(ch[ii] + " ");
        }
        System.out.println();

        for (int jj = 0; jj < S.length(); jj++) {
            int ch_index = (int)(S.charAt(jj) - 'a');
            if (ch[ch_index] != 0) {
                for (int cnt = 0; cnt < ch[ch_index]; cnt++) {
                    sb.append(S.charAt(jj));
                }
                ch[ch_index] = 0;
            }
        }

        for (int index = 0; index < 26; index++) {
            if (ch[index] != 0) {
                for (int cnt = 0; cnt < ch[index]; cnt++) {
                    sb.append((char)('a' + index));
                }
            }
        }


        return sb.toString();
    }

    public static void main(String[] args) {
        String S = "cba";
        String T = "adfwerfaabbccxyz";
        System.out.println(customSortString(S, T));
    }
}
